package com.ruoyi.web.admin.controller.system;

import java.util.List;

import com.ruoyi.system.domain.District;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IDistrictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

/**
 * 行政区划信息操作处理
 *
 * @author ruoyi
 */
@Api(tags = "行政区划管理")
@RestController
@RequestMapping("/system/district")
public class DistrictController extends BaseController {

    private final IDistrictService districtService;

    public DistrictController(IDistrictService districtService) {
        this.districtService = districtService;
    }

    /**
     * 获取行政区划列表
     */
    @ApiOperation(value = "获取行政区划列表", notes = "获取所有行政区划信息")
    @PreAuthorize("@ss.hasPermi('system:district:list')")
    @GetMapping("/page")
    public TableDataInfo page(District district) {
        startPage();
        List<District> list = districtService.selectDistrictList(district);
        return getDataTable(list);
    }

    /**
     * 获取行政区划列表
     */
    @ApiOperation(value = "获取行政区划列表", notes = "获取所有行政区划信息")
    @PreAuthorize("@ss.hasPermi('system:district:list')")
    @GetMapping("/list")
    public AjaxResult list(District district) {
        List<District> districts = districtService.selectDistrictList(district);
        return success(districts);
    }

    /**
     * 根据行政区划编号获取详细信息
     */
    @ApiOperation(value = "获取行政区划详细信息", notes = "根据id获取行政区划详细信息")
    @ApiImplicitParam(name = "id", value = "行政区划ID", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermi('system:district:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(districtService.selectDistrictById(id));
    }

    /**
     * 新增行政区划
     */
    @ApiOperation(value = "新增行政区划", notes = "新增行政区划信息")
    @PreAuthorize("@ss.hasPermi('system:district:add')")
    @Log(title = "行政区划管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody District district) {
        return toAjax(districtService.insertDistrict(district));
    }

    /**
     * 修改行政区划
     */
    @ApiOperation(value = "修改行政区划", notes = "修改行政区划信息")
    @PreAuthorize("@ss.hasPermi('system:district:edit')")
    @Log(title = "行政区划管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody District district) {
        return toAjax(districtService.updateDistrict(district));
    }

    /**
     * 删除行政区划
     */
    @ApiOperation(value = "批量删除行政区划", notes = "批量删除行政区划信息")
    @ApiImplicitParam(name = "ids", value = "行政区划ID数组", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long[].class)
    @PreAuthorize("@ss.hasPermi('system:district:remove')")
    @Log(title = "行政区划管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(districtService.deleteDistrictByIds(ids));
    }

    /**
     * 删除单个行政区划
     */
    @ApiOperation(value = "删除单个行政区划", notes = "删除单个行政区划信息")
    @ApiImplicitParam(name = "id", value = "行政区划ID", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermi('system:district:remove')")
    @Log(title = "行政区划管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(districtService.deleteDistrictById(id));
    }
}